<?php
/*
 * @o: ========================================================
 * @Created By: VsCode
 * @Author: Fu
 * @name: 管理员控制器
 * @LastEditTime: 2021-05-13 08:38:25
 * @Date: 2021-04-02 18:13:40
 * @u: ========================================================
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\goods\Goods_category as GoodsCategoryModel;
use app\admin\model\goods\Goods as GoodsModel;
use think\Db;
/**
 * 首页接口
 */
class Category extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    public function  categorylist(GoodsCategoryModel $GoodsCategoryModel)
    {
        $field = 'id,title';
        $GoodsCategoryList = $GoodsCategoryModel->getAllList($field)->toArray();
        $this->success('成功',$GoodsCategoryList);
    }


    public function goods()
    {
        $param = input('param.');

        // $field = 'id,goods_image,goods_name,goods_rice';
        $GoodsList = Db::name('goods')
                    ->alias('a')
                    ->join('fa_order_child oc','a.id = oc.goods_id','LEFT')
                    ->field(['a.id, a.goods_name, a.goods_image, a.goods_rice, sum(oc.goods_id) as count'])
                    ->group('a.id')
                    ->where('goods_category_id',$param['id'])
                    ->where('goodsswitch',1)
                    ->select();            
        $goods_rice = array_column($GoodsList,'goods_rice');
        $count = array_column($GoodsList,'count');
        if($param['sort'] == 1){
            array_multisort($goods_rice,SORT_DESC,$GoodsList);
        }else if($param['sort'] == 2){
            array_multisort($goods_rice,SORT_ASC,$GoodsList);
        }else if($param['sort'] == 3){
            array_multisort($count,SORT_DESC,$GoodsList);
        }else if($param['sort'] == 4){
            array_multisort($count,SORT_ASC,$GoodsList);
        }
        $this->success('成功',$GoodsList);
    }

    public function goodsdt()
    {
        $param = input('param.');
        // print_r($param);exit;
        $goodsdt = Db::name('goods')->where('id',$param['id'])->find();
        $goodsdt['count'] = Db::name('order_child')->where('goods_id',$goodsdt['id'])->count();
        $this->success('成功',$goodsdt);
    }
    
}
